package com.maila.xiaoaoschool.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.google.gson.Gson;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.adapter.MainLocationAdapter;
import com.maila.xiaoaoschool.api.IndexApi;
import com.maila.xiaoaoschool.entity.IndexRecommendShop;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.maila.xiaoaoschool.util.StartActivityUtils;
import com.maila.xiaoaoschool.widget.NoScrollListView;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/28.
 */

public class AllFoodsActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.tv_shop_namee)
    TextView tvShopNamee;
    @BindView(R.id.rb_zh)
    RadioButton rbZh;
    @BindView(R.id.rb_xl)
    RadioButton rbXl;
    @BindView(R.id.rb_jl)
    RadioButton rbJl;
    @BindView(R.id.lv_fujin)
    NoScrollListView lvFujin;
    @BindView(R.id.hot_refresh)
    MaterialRefreshLayout hotRefresh;
    MainLocationAdapter mainLocationAdapter;
    Gson gson;
    private String lat="36.71287";
    private String lng="119.126626";
    private String adcode="1";
    private int page=1;
    private int size=6;
    private String field="evaluate";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_foods);
        ButterKnife.bind(this);
        gson=new Gson();
        mainLocationAdapter=new MainLocationAdapter(this);
        lvFujin.setAdapter(mainLocationAdapter);
        lvFujin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                StartActivityUtils.start(AllFoodsActivity.this, ShopActivity.class);
            }
        });
        hotRefresh.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                page=1;
                initlocationshop();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                initlocationshop();
            }
        });
        initlocationshop();
    }
    private void requestViewFoucus(View view) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
    }

    private void initlocationshop() {
        new IndexApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                IndexRecommendShop recommendShop=gson.fromJson(result,IndexRecommendShop.class);
                if (recommendShop.getResult()==1) {
                    if(page==1){
                        mainLocationAdapter.setMdata(recommendShop.getData());
                    }else {
                        mainLocationAdapter.addMdata(recommendShop.getData());
                    }
                    hotRefresh.finishRefreshLoadMore();
                    ++page;

                }else {
                    hotRefresh.finishRefreshLoadMore();
                    hotRefresh.setLoadMore(false);
                    Toast.makeText(AllFoodsActivity.this,"暂无更多店铺",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getIndexShop(lat,lng,page,size,field,"desc",adcode);
    }
    @Override
    protected void onResume() {
        super.onResume();
        requestViewFoucus(tvShopNamee);
    }

    private void reset_rb() {
        rbJl.setChecked(false);
        rbXl.setChecked(false);
        rbZh.setChecked(false);
    }
    @OnClick({R.id.white_return_iv, R.id.rb_zh, R.id.rb_xl, R.id.rb_jl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;

            case R.id.rb_zh:
                reset_rb();
                rbZh.setChecked(true);
                field="evaluate";
                page=1;
                initlocationshop();
                break;
            case R.id.rb_xl:
                reset_rb();
                rbXl.setChecked(true);
                field="sellcount";
                page=1;
                initlocationshop();
                break;
            case R.id.rb_jl:
                reset_rb();
                rbJl.setChecked(true);
                field="range";
                page=1;
                initlocationshop();
                break;
        }
    }
}
